package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.AuthorityDto;
import com.hualude.roadsign.dto.HighLeverDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.repository.CountRepository;
import com.hualude.roadsign.repository.HighLeverFaultCountRepository;
import com.hualude.roadsign.service.HighLeverLocationCountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xu_jiaHui on 2018/3/24.
 */
@Service
public class HighLeverLocationCountServiceImpl implements HighLeverLocationCountService {
    @Resource
    private HighLeverFaultCountRepository highLeverFaultCountRepository;
    @Resource
    private CountRepository countRepository;

    public DTResponseDto getHighLever4DT(Integer userId, Integer orgId, DTRequestDto dtRequestDto) throws Exception {
        AuthorityDto authorityDto = new AuthorityDto(userId, orgId);
        authorityDto = getActorAndOrgIds(authorityDto);
        Integer count = highLeverFaultCountRepository.getHighLeverCount4DT(dtRequestDto, authorityDto.getActor(), authorityDto.getOrgIds());
        List<HighLeverDto> dto4DTs = highLeverFaultCountRepository.getHighLever4DT(dtRequestDto, authorityDto.getActor(), authorityDto.getOrgIds());
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        return dtResponseDto;
    }

    public List<String> getProvinceName(Integer userId, Integer orgId) throws Exception {
        AuthorityDto authorityDto = new AuthorityDto(userId, orgId);
        authorityDto = getActorAndOrgIds(authorityDto);
        return highLeverFaultCountRepository.getProvinceName(authorityDto.getOrgIds());
    }

    public List<String> getCityName(Integer userId, Integer orgId, String provinceName) throws Exception {
        AuthorityDto authorityDto = new AuthorityDto(userId, orgId);
        authorityDto = getActorAndOrgIds(authorityDto);
        return highLeverFaultCountRepository.getCityName(authorityDto.getOrgIds(), provinceName);
    }

    public List<String> getAllAreaName(Integer userId, Integer orgId, String provinceName, String cityName) throws Exception {
        AuthorityDto authorityDto = new AuthorityDto(userId, orgId);
        authorityDto = getActorAndOrgIds(authorityDto);
        return highLeverFaultCountRepository.getAllAreaName(authorityDto.getOrgIds(), provinceName, cityName);
    }

    public List<String> getAllRoadName(String provinceName, String cityName, String areaName, Integer userId, Integer orgId) throws Exception {
        AuthorityDto authorityDto = new AuthorityDto(userId, orgId);
        authorityDto = getActorAndOrgIds(authorityDto);
        return highLeverFaultCountRepository.getRoadNames(provinceName, cityName, areaName, authorityDto.getOrgIds(), authorityDto.getActor());
    }

    public List<String> getStartRoadName(Integer userId, Integer orgId, String provinceName, String cityName, String areaName,
                                         String endRoadName, String roadName) throws Exception {
        AuthorityDto authorityDto = new AuthorityDto(userId, orgId);
        authorityDto = getActorAndOrgIds(authorityDto);
        List<String> roadStartName = highLeverFaultCountRepository.getStartRoadName(authorityDto.getOrgIds(), provinceName, cityName,
                areaName, endRoadName, roadName);
        return roadStartName;
    }

    public List<String> getEndRoadName(Integer userId, Integer orgId, String startRoad, String provinceName, String cityName,
                                       String areaName, String roadName) throws Exception {
        AuthorityDto authorityDto = new AuthorityDto(userId, orgId);
        authorityDto = getActorAndOrgIds(authorityDto);
        return highLeverFaultCountRepository.getEndRoadName(authorityDto.getOrgIds(), startRoad, provinceName, cityName, areaName, roadName);
    }

    public List<Integer> getHighLeverTypeCounts(Integer userId, Integer orgId, String time, String province, String city, String area, String road) throws Exception {
        if (province.equals("-1")) province = "";
        if (city.equals("-1")) city = "";
        if (area.equals("-1")) area = "";
        if (road.equals("-1")) road = "";
        AuthorityDto authorityDto = new AuthorityDto(userId, orgId);
        authorityDto = getActorAndOrgIds(authorityDto);
        String orgIds = orgId.toString();
        if (orgId == -1) {
            if (authorityDto.getActor() == 7 || authorityDto.getActor() == 1) {
                orgIds = authorityDto.getOrgIds();
            }
        }
        return highLeverFaultCountRepository.getHighLeverTypeCounts(orgIds, time, province, city, area, road);
    }

    //得到用户公司及其子公司IDs，用户最终角色
    public AuthorityDto getActorAndOrgIds(AuthorityDto authorityDto) {
        try {
            String actorIds = countRepository.getactorIds(authorityDto.getUserId());
            String orgIds = authorityDto.getOrgId().toString();
            if (orgIds.equals("-1")) {
                orgIds = countRepository.getRealOrgIds(authorityDto.getUserId()).toString();
            }
            Integer actor = Integer.parseInt(actorIds.split(",")[0]);//用户最终角色 1系统>7主管>其它
            Integer isManager = actorIds.indexOf("1");//系统管理员
            Integer isChief = actorIds.indexOf("7");//主管管理员
            if (isManager >= 0) {
                actor = 1;
                String ChildOrgIds = countRepository.getOrgIds();
                if (!ChildOrgIds.equals("")) {
                    orgIds = ChildOrgIds;
                }
            } else if (isChief >= 0) {
                actor = 7;
                String ChildOrgIds = countRepository.getChildOrgIds(Integer.parseInt(orgIds));
                if (!ChildOrgIds.equals("")) {
                    orgIds += "," + ChildOrgIds;
                }
            }
            authorityDto.setActor(actor);
            authorityDto.setOrgIds(orgIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return authorityDto;
    }

}

